// pages/search/search.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchKey:"",
    keyWord1:"",
    description1:"",
    showAddModal: false,
  },
  searchInput: function(e) {
   let value =  e.detail.value
    this.setData({
      searchKey: value
    })
  },
  search: function (e) {
  let searchKey =  this.data.searchKey
   if  (searchKey == ''){
    this.setData({
      keyWord: searchKey,
      description: "脑袋空空！",
    })
    return 
   }
    var db = wx.cloud.database()
    db.collection('punchline').where({
      keyWord : db.RegExp({
        regexp: searchKey,
        options: 'i',
      })
    }).get().then(res => {
      console.log(res)
      if (res.data.length == 0) {
        this.setData({
          keyWord1: searchKey,
          description1: "脑袋空空！",
        })
        return 
      }

      // 只拿前两个数据
      var total = res.data.length

      var _collections = new Array()
      for (var i = 0; i < total; i++) {
        _collections.push(JSON.parse(JSON.stringify(res.data[i])))
      }
      
      this.setData({
        keyWord1 : _collections[0].keyWord,
        description1: _collections[0].description
      })

    }).catch(res=>{
      console.log(res)
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
    this.search(e)
  },  
  hideModal(e) {
    this.setData({
      modalName: null,
      keyWord1: "",
      description1: "",
      keyWord2: "",
      description2:"",
      showSecondView:null
    })
  },
})